$(function(){
    "use strict";
    let href = window.location.href;
    let Type_hrefINdex = href.indexOf('type=');
    let pro_id_hrefINdex = href.indexOf('pro_id=');
    let pro_status_hrefINdex = href.indexOf('pro_status=');
    let type_id = href.substring(Type_hrefINdex+1);
    let pro_id = href.substring(pro_id_hrefINdex+7);
    let pro_status = href.substring(pro_status_hrefINdex+11);
    let Completed_file = []
    let file5 = []
    let file6 = []
    let file7 = []
    let file8 = []
    let file9 = []
    let file10 = []

    if(pro_status==0){
        $('#proStatus').children(':first-child').children('i').addClass('icon-Raidobox-selectedRai').removeClass('icon-Raidobox')
        $('#proStatus').children(':last-child').children('i').removeClass('icon-Raidobox-selectedRai').addClass('icon-Raidobox')
        $('#proStatus').attr('data-value',0)
    }else{
        $('#proStatus').children(':last-child').children('i').addClass('icon-Raidobox-selectedRai').removeClass('icon-Raidobox')
        $('#proStatus').children(':first-child').children('i').removeClass('icon-Raidobox-selectedRai').addClass('icon-Raidobox')
        $('#proStatus').attr('data-value',1)
    }
    $('#proStatus').on('click','li',function(e){
        e.preventDefault();
        $(this).children('i').addClass('icon-Raidobox-selectedRai').removeClass('icon-Raidobox')
        $(this).siblings().children('i').removeClass('icon-Raidobox-selectedRai').addClass('icon-Raidobox')
        if($(this).children('span').html()=='未完成(阶段性验收)'){
            $('#proStatus').attr('data-value',0)
        }else if($(this).children('span').html()=='已完成(结题验收)'){
            $('#proStatus').attr('data-value',1)
        }
    })

    if(Type_hrefINdex>0){
        $.ajax({
            url:'/Admin/ProCheck/check',
            type:'get',
            data:{
                id:pro_id
            },
            success:function(data){
                if(data.code==1){
                    if(data.status==0){
                        $('.modifyInfo_btns').html('保存');
                        $('.CompanyInfoList input').attr('readonly',false);
                        $('.CompanyInfoList input').addClass('active');
                        $('.textarea_box').children('textarea').addClass('active');
                        $('.textarea_box').children('p').removeClass('active');
                        $('.file_box').children('div').addClass('active')
                        $('.file_box').children('a').removeClass('active')

                        $('#choose_time').children('input').addClass('time_choose');
                        $('#choose_time').children('input').attr('disabled',false);
                        laydate.render({
                            elem: '.time_choose', //指定元素
                            theme: '#2881C1',
                            // show: true, //直接显示
                            closeStop: '#choose_time',
                            done: function(value, date, endDate){
                            }
                        });
                    }else{
                        // console.log('edit')
                        $('.CompanyInfoList input').attr('readonly',true);
                        $('.CompanyInfoList input').removeClass('active');
                        $('.modifyInfo_btns').html('编辑');
                        // var conclusion = $('.textarea_box>textarea.conclusion').val();
                        // var content = $('.textarea_box>textarea.content').val();
                        // $('.textarea_box>textarea.conclusion').siblings('p').html(conclusion);
                        // $('.textarea_box>textarea.content').siblings('p').html(content);
                        $('.textarea_box>p').addClass('active');
                        $('.textarea_box>textarea').removeClass('active');

                        
                        // var fileVal = $('.hidden_input').parent().siblings('span').html()

                        let datas = data.data
                        $('.CompanyInfoList').find('[data-value=Completed_time]').val(datas.check_time)
                        $('.CompanyInfoList').find('[data-value=Completed_place]').val(datas.check_addr)
                        $('.CompanyInfoList').find('[data-value=Completed_people]').val(datas.check_peo)
                        $('.CompanyInfoList').find('[data-value=Completed_money]').val(datas.check_total)
                        $('.CompanyInfoList').find('textarea[data-value=Completed_conent]').val(datas.check_content)
                        $('.CompanyInfoList').find('p[data-value=Completed_conent]').html(datas.check_content)
                        $('.CompanyInfoList').find('textarea[data-value=Completed_conclusion]').val(datas.check_res)
                        $('.CompanyInfoList').find('p[data-value=Completed_conclusion]').html(datas.check_res)
                        // $('.CompanyInfoList').find('[data-value=Completed_file]').parent().siblings('span').html(datas.check_fname+'<i class="iconfont icon-delete delete_logo"></i>')
                        // $('.CompanyInfoList').find('[data-value=file5]').parent().siblings('span').html(datas.kj_fname+'<i class="iconfont icon-delete delete_logo"></i>')
                        // $('.file_box').children('a').html(datas.check_fname)
                        // $('.file_box').children('a').attr('href',datas.check_fkey)
                        $('#file4').parent().parent().siblings('a').html(datas.check_fname)
                        $('#file4').parent().parent().siblings('a').attr('href',datas.check_fkey)
                        if(datas.check_fname!=''){
                            $('#file4').parent().siblings('span').html(`<div><span class="baogao_name">${datas.check_fname}</span><span class="baogao_size">${datas.check_fsize}KB</span> <i class="iconfont icon-delete delete_logo"></i></div>`)
                        }

                        $('#file5').parent().parent().siblings('a').html(datas.kj_fname)
                        $('#file5').parent().parent().siblings('a').attr('href',datas.kj_fkey)
                        if(datas.kj_fname!=''){
                            $('#file5').parent().siblings('span').html(`<div><span class="baogao_name">${datas.kj_fname}</span><span class="baogao_size">${datas.kj_fsize}KB</span> <i class="iconfont icon-delete delete_logo"></i></div>`)
                        }

                        $('#file6').parent().parent().siblings('a').html(datas.ss_fname)
                        $('#file6').parent().parent().siblings('a').attr('href',datas.ss_fkey)
                        if(datas.ss_fname!=''){
                            $('#file6').parent().siblings('span').html(`<div><span class="baogao_name">${datas.ss_fname}</span><span class="baogao_size">${datas.ss_fsize}KB</span> <i class="iconfont icon-delete delete_logo"></i></div>`)
                        }

                        $('#file7').parent().parent().siblings('a').html(datas.dht_fname)
                        $('#file7').parent().parent().siblings('a').attr('href',datas.dht_fkey)
                        if(datas.dht_fname!=''){
                            $('#file7').parent().siblings('span').html(`<div><span class="baogao_name">${datas.dht_fname}</span><span class="baogao_size">${datas.dht_fsize}KB</span> <i class="iconfont icon-delete delete_logo"></i></div>`)
                        }

                        $('#file8').parent().parent().siblings('a').html(datas.dsm_fname)
                        $('#file8').parent().parent().siblings('a').attr('href',datas.dsm_fkey)
                        if(datas.dsm_fname!=''){
                            $('#file8').parent().siblings('span').html(`<div><span class="baogao_name">${datas.dsm_fname}</span><span class="baogao_size">${datas.dsm_fsize}KB</span> <i class="iconfont icon-delete delete_logo"></i></div>`)
                        }

                        $('#file9').parent().parent().siblings('a').html(datas.wht_fname)
                        $('#file9').parent().parent().siblings('a').attr('href',datas.wht_fkey)
                        if(datas.wht_fname!=''){
                            $('#file9').parent().siblings('span').html(`<div><span class="baogao_name">${datas.wht_fname}</span><span class="baogao_size">${datas.wht_fsize}KB</span> <i class="iconfont icon-delete delete_logo"></i></div>`)
                        }

                        $('#file10').parent().parent().siblings('a').html(datas.wmx_fname)
                        $('#file10').parent().parent().siblings('a').attr('href',datas.wmx_fkey)
                        if(datas.wmx_fname!=''){
                            $('#file10').parent().siblings('span').html(`<div><span class="baogao_name">${datas.wmx_fname}</span><span class="baogao_size">${datas.wmx_fsize}KB</span> <i class="iconfont icon-delete delete_logo"></i></div>`)
                        }

                        // Completed_file = []
                        // Completed_file.push(datas.base64)
                        // Completed_file.push(datas.check_fname)
                        // Completed_file.push(datas.check_fsize)
                        $('.file_box').children('div').removeClass('active')
                        $('.file_box').children('a').addClass('active')

                        $('#choose_time').children('input').removeClass('time_choose');
                        $('#choose_time').children('input').attr('disabled',true);
                        $('.modifyInfo_btns').addClass('active')
                    }
                }else{
                    $('#yz_modal').html(data.msg)
                    $('#yz_modal').addClass('active')
                    tipShow()
                }
            }
        })
    }else{
        $('.modifyInfo_btns').html('保存');
        $('.CompanyInfoList input').attr('readonly',false);
        $('.CompanyInfoList input').addClass('active');
        $('.textarea_box').children('textarea').addClass('active');
        $('.textarea_box').children('p').removeClass('active');
        $('.file_box').children('div').addClass('active')
        $('.file_box').children('a').removeClass('active')

        $('#choose_time').children('input').addClass('time_choose');
        $('#choose_time').children('input').attr('disabled',false);
        laydate.render({
            elem: '.time_choose', //指定元素
            theme: '#2881C1',
            // show: true, //直接显示
            closeStop: '#choose_time',
            done: function(value, date, endDate){
//                var dateVal = date.year+'年'+date.month+'月'
//                window.query_time = dateVal
            }
        });
    }



    // 编辑和保存来回切换
    $('.modifyInfo_btns').click(function(e){
        e.preventDefault();
        // $('.CompanyInfoList input').toggleClass('active');
        // console.log($('.modifyInfo_btns').hasClass('active'))
        if($('.modifyInfo_btns').hasClass('active')){
            // $('.CompanyInfoList input').attr('readonly',false);
            // $('.CompanyInfoList input').attr('readonly',false);
            // $('.CompanyInfoList input').removeAttr('readonly');
            // $(this).html('保存');
            // $('.textarea_box').children('textarea').addClass('active');
            // $('.textarea_box').children('p').removeClass('active');
            // $('.file_box').children('div').addClass('active')
            // $('.file_box').children('a').removeClass('active')

            $('#choose_time').children('input').addClass('time_choose');
            $('#choose_time').children('input').attr('disabled',false);
            
            $('.modifyInfo_btns').html('保存');
            $('.CompanyInfoList input').attr('readonly',false);
            $('.CompanyInfoList input').addClass('active');
            $('.textarea_box').children('textarea').addClass('active');
            $('.textarea_box').children('p').removeClass('active');
            $('.file_box').children('div').addClass('active')
            $('.file_box').children('a').removeClass('active')

            laydate.render({
                elem: '.time_choose', //指定元素
                theme: '#2881C1',
                // show: true, //直接显示
                closeStop: '#choose_time',
                done: function(value, date, endDate){
//                var dateVal = date.year+'年'+date.month+'月'
//                window.query_time = dateVal
                }
            });
            $('.modifyInfo_btns').removeClass('active')

        }else{
            let status = $('#proStatus').attr('data-value')

            let Completed_time = $('.CompanyInfoList').find('[data-value=Completed_time]').val()
            let Completed_place = $('.CompanyInfoList').find('[data-value=Completed_place]').val()
            let Completed_people = $('.CompanyInfoList').find('[data-value=Completed_people]').val()
            let Completed_money = $('.CompanyInfoList').find('[data-value=Completed_money]').val()
            let Completed_conent = $('.CompanyInfoList').find('[data-value=Completed_conent]').val()
            let Completed_conclusion = $('.CompanyInfoList').find('[data-value=Completed_conclusion]').val()
            // let Completed_file = $('.CompanyInfoList').find('[data-value=Completed_file]').val()
            if(Completed_time == '' || Completed_place == '' || Completed_people == '' || Completed_money == '' || Completed_conent == '' || Completed_conclusion == ''){
                $('#yz_modal').html('内容未填写完整')
                $('#yz_modal').addClass('active')
                tipShow()
                return
            }else{
                let that = this
                $.ajax({
                    url:'/Admin/ProCheck/check',
                    type:'post',
                    data:{
                        pro_id:pro_id,
                        check_time: Completed_time,
                        check_addr: Completed_place,
                        check_peo: Completed_people,
                        check_content: Completed_conent,
                        check_res: Completed_conclusion,
                        // check_fkey:Completed_file[0],
                        // check_fname:Completed_file[1],
                        // check_fsize:Completed_file[2],
                        check_total:Completed_money,
                        status:status
                    },
                    success:function(data){
                        if(data.code==1){
                            $('#yz_modal').html('验收成功')
                            $('#yz_modal').addClass('active')
                            tipShow()

                            // $('.CompanyInfoList input').attr('readonly',true);
                            $(that).html('编辑');
                            // var conclusion = $('.textarea_box>textarea.conclusion').val();
                            // var content = $('.textarea_box>textarea.content').val();
                            // $('.textarea_box>textarea.conclusion').siblings('p').html(conclusion);
                            // $('.textarea_box>textarea.content').siblings('p').html(content);
                            // $('.textarea_box>p').addClass('active');
                            // $('.textarea_box>textarea').removeClass('active');

                            // $('.file_box').children('div').removeClass('active')
                            // $('.file_box').children('a').addClass('active')
                            // var fileVal = $('.hidden_input').parent().siblings('span').html()
                            // $('.file_box').children('a').html(fileVal)

                            // $('#choose_time').children('input').removeClass('time_choose');
                            // $('#choose_time').children('input').attr('disabled',true);

                            // console.log(1111)
                            $('.modifyInfo_btns').addClass('active')
                            window.location.href='/Admin/ProcessIndex/checkIndex'

                            
                        }else{
                            $('#yz_modal').html(data.msg)
                            $('#yz_modal').addClass('active')
                            tipShow()
                        }
                    }
                })

            }

        }
    });

    // 渲染PDF
    $('.file_box').on('click','a',function(e){
        e.preventDefault()
        e.stopPropagation()
        let purl = $(this).attr('href');//要展示的文件路径
        // console.log(purl)
        // let purl = 'JavaScript高级程序设计（第3版）中文 高清 完整.pdf'
        // 下面代码都是处理IE浏览器的情况
        if (window.ActiveXObject || "ActiveXObject" in window) {
            //判断是否为IE浏览器，"ActiveXObject" in window判断是否为IE11
            //判断是否安装了adobe Reader
            for (x = 2; x < 10; x++) {
                try {
                    oAcro = eval("new ActiveXObject('PDF.PdfCtrl." + x + "');");
                    if (oAcro) {
                        flag = true;
                    }
                } catch (e) {
                    flag = false;
                }
            }
            try {
                oAcro4 = new ActiveXObject('PDF.PdfCtrl.1');
                if (oAcro4) {
                    flag = true;
                }
            } catch (e) {
                flag = false;
            }

            try {
                oAcro7 = new ActiveXObject('AcroPDF.PDF.1');
                if (oAcro7) {
                    flag = true;
                }
            } catch (e) {
                flag = false;
            }

            if (flag) {//支持
                pdfShow(purl);//调用显示的方法

            }else {//不支持
                $("#pdfContent").append("对不起,您还没有安装PDF阅读器软件呢,为了方便预览PDF文档,请选择安装！");
                alert("对不起,您还没有安装PDF阅读器软件呢,为了方便预览PDF文档,请选择安装！");
                location = "http://ardownload.adobe.com/pub/adobe/reader/win/9.x/9.3/chs/AdbeRdr930_zh_CN.exe";
            }

        }else {
            pdfShow(purl);//调用显示的方法
            $("#PDFBox").addClass('active')
        }
    })
    // 显示pdf
    function pdfShow(url){
        $("#PDFBox").append('<iframe style="height:700px;width:700px;margin:0 auto;overflow: hidden" src="'+url+'"></iframe>');
    }

    // 当点击其他地方，关闭pdf
    //当点击区域不在列表选择范围内则关闭弹框
    $(document).click(function(e){
        var $this = $(e.target);
        if($this.parent().attr('id') != "PDFBox"){
            $('#PDFBox').removeClass('active');
            $("#PDFBox").html('')
        }
    });
    // 点击上传按钮
    $('.hidden_input').on('change',function(){
        let data = this.files;
        for(var i =0;i<data.length;i++){
            let fileReader = new FileReader();
            fileReader.readAsDataURL(data[i]);
            let size = Math.round(data[i].size/1024);
            let name = data[i].name;
            $(this).parent().siblings('span').css({'display':'flex !important','flex-direction':'column','justify-content':'space-between'})
            let spanHTML = `<div>
                                <span class="baogao_name">${name}</span>
                                <span class="baogao_size">${size}KB</span>
                                <i class="iconfont icon-delete delete_logo"></i>
                            </div>
                            <div class='progress'>
                                <div class='progressBar'></div>
                            </div>
                            `;
            $(this).parent().siblings('span').html(spanHTML)
            let that = this
            // let spanHTML = `<span class="baogao_name">${name}</span><span class="baogao_size">${size}KB</span><i class="iconfont icon-delete delete_logo"></i>`;
            // $(this).parent().siblings('span').html(spanHTML)
            // $(this).attr('ValName',name)
            // console.log($(this))
            //定时器控制进度条跑呀跑呀跑呀~
            let n = 0
            $(this).parent().siblings('span').find('div.progressBar').css({'width':n+'%'})
            // console.log(n)
            let progressBar = setInterval(function(){
                n+=5
                // console.log(n)
                 $(that).parent().siblings('span').find('div.progressBar').css({'width':n+'%'})
                // $(this).parent().siblings('span').find('div.progressBar').css({'width':'10%'})
                if(n>85){
                    clearTimeout(progressBar)
                }
            },150)

            fileReader.onload = function (e){
                let url = this.result;//上传文件的URL
                let inputData = $(that).attr('data-value')
                if(inputData=='Completed_file'){
                    Completed_file = []
                    Completed_file.push(url);
                    Completed_file.push(name);
                    Completed_file.push(size);
                    $.ajax({
                        url:'/Admin/Project/uploadFile',
                        type:'post',
                        data:{
                            type: 4,
                            files: Completed_file
                        },
                        success:function(data){
                            if(data.code==1){
                                clearTimeout(progressBar)
                                $(that).parent().siblings('span').find('div.progressBar').css({'width':'100%'})
                            }
                        }
                    })
                }else if(inputData=='file5'){
                    file5 = []
                    file5.push(url);
                    file5.push(name);
                    file5.push(size);
                    $.ajax({
                        url:'/Admin/Project/uploadFile',
                        type:'post',
                        data:{
                            type: 5,
                            files: file5
                        },
                        success:function(data){
                            if(data.code==1){
                                clearTimeout(progressBar)
                                $(that).parent().siblings('span').find('div.progressBar').css({'width':'100%'})
                            }
                        }
                    })
                }else if(inputData=='file6'){
                    file6 = []
                    file6.push(url);
                    file6.push(name);
                    file6.push(size);
                    $.ajax({
                        url:'/Admin/Project/uploadFile',
                        type:'post',
                        data:{
                            type: 6,
                            files: file6
                        },
                        success:function(data){
                            if(data.code==1){
                                clearTimeout(progressBar)
                                $(that).parent().siblings('span').find('div.progressBar').css({'width':'100%'})
                            }
                        }
                    })
                }else if(inputData=='file7'){
                    file7 = []
                    file7.push(url);
                    file7.push(name);
                    file7.push(size);
                    $.ajax({
                        url:'/Admin/Project/uploadFile',
                        type:'post',
                        data:{
                            type: 7,
                            files: file7
                        },
                        success:function(data){
                            if(data.code==1){
                                clearTimeout(progressBar)
                                $(that).parent().siblings('span').find('div.progressBar').css({'width':'100%'})
                            }
                        }
                    })
                }else if(inputData=='file8'){
                    file8 = []
                    file8.push(url);
                    file8.push(name);
                    file8.push(size);
                    $.ajax({
                        url:'/Admin/Project/uploadFile',
                        type:'post',
                        data:{
                            type: 8,
                            files: file8
                        },
                        success:function(data){
                            if(data.code==1){
                                clearTimeout(progressBar)
                                $(that).parent().siblings('span').find('div.progressBar').css({'width':'100%'})
                            }
                        }
                    })
                }else if(inputData=='file9'){
                    file9 = []
                    file9.push(url);
                    file9.push(name);
                    file9.push(size);
                    $.ajax({
                        url:'/Admin/Project/uploadFile',
                        type:'post',
                        data:{
                            type: 9,
                            files: file9
                        },
                        success:function(data){
                            if(data.code==1){
                                clearTimeout(progressBar)
                                $(that).parent().siblings('span').find('div.progressBar').css({'width':'100%'})
                            }
                        }
                    })
                }else if(inputData=='file10'){
                    file10 = []
                    file10.push(url);
                    file10.push(name);
                    file10.push(size);
                    $.ajax({
                        url:'/Admin/Project/uploadFile',
                        type:'post',
                        data:{
                            type: 10,
                            files: file10
                        },
                        success:function(data){
                            if(data.code==1){
                                clearTimeout(progressBar)
                                $(that).parent().siblings('span').find('div.progressBar').css({'width':'100%'})
                            }
                        }
                    })
                }
            }
        }
    })

    $('.CompanyInfoList').find('.Application_baogao').on('click','i.delete_logo',function(e){
        let data_value = $(this).parent().parent().siblings().children().attr('data-value')
        console.log(data_value)
        if(data_value=='Completed_file'){
            Completed_file = []
            let that = this
            $.ajax({
                url:'/Admin/Project/delFile',
                type:'post',
                data:{
                    type: 4,
                    id:pro_id
                },
                success:function(data){
                    if(data.code==1){
                        $(that).parent().parent().remove()
                    }
                }
            })
        }else if(data_value=='file5'){
            file5 = []
            let that = this
            $.ajax({
                url:'/Admin/Project/delFile',
                type:'post',
                data:{
                    type: 5,
                    id:pro_id
                },
                success:function(data){
                    if(data.code==1){
                        $(that).parent().parent().remove()
                    }
                }
            })
        }else if(data_value=='file6'){
            file6 = []
            let that = this
            $.ajax({
                url:'/Admin/Project/delFile',
                type:'post',
                data:{
                    type: 6,
                    id:pro_id
                },
                success:function(data){
                    if(data.code==1){
                        $(that).parent().parent().remove()
                    }
                }
            })
        }else if(data_value=='file7'){
            file7 = []
            let that = this
            $.ajax({
                url:'/Admin/Project/delFile',
                type:'post',
                data:{
                    type: 7,
                    id:pro_id
                },
                success:function(data){
                    if(data.code==1){
                        $(that).parent().parent().remove()
                    }
                }
            })
        }else if(data_value=='file8'){
            file8 = []
            let that = this
            $.ajax({
                url:'/Admin/Project/delFile',
                type:'post',
                data:{
                    type: 8,
                    id:pro_id
                },
                success:function(data){
                    if(data.code==1){
                        $(that).parent().parent().remove()
                    }
                }
            })
        }else if(data_value=='file9'){
            file9 = []
            let that = this
            $.ajax({
                url:'/Admin/Project/delFile',
                type:'post',
                data:{
                    type: 9,
                    id:pro_id
                },
                success:function(data){
                    if(data.code==1){
                        $(that).parent().parent().remove()
                    }
                }
            })
        }else if(data_value=='file10'){
            file10 = []
            let that = this
            $.ajax({
                url:'/Admin/Project/delFile',
                type:'post',
                data:{
                    type: 10,
                    id:pro_id
                },
                success:function(data){
                    if(data.code==1){
                        $(that).parent().parent().remove()
                    }
                }
            })
        }
        // $(this).parent().parent().remove()

    })

    // 弹出来的小框框
    function tipShow(){
        window.tipBox = setTimeout(function(){
            // $('#yz_modal').removeClass('active')
            // $('#yz_modal').addClass('fadeOut')
            $('#yz_modal').removeClass('active')
            clearTimeout(tipBox)
        },2000)
    }
})